package Presentacion.Precios;

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.util.StringTokenizer;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import Fisica.Cooperativa.ProcCoope;
import Fisica.Precios.FisicaPrecios;
import Logica.CodigoBarra;
import Logica.Cooperativa.Articulo;
import Presentacion.Inicio.Principal;

import javax.swing.SwingConstants;

public class PreciosArticulos extends JFrame {

	
	private JPanel contentPane;
	private JTextField tfCBarra;
	private JTextField tfPrecio;
	private JTextArea tfDescripcion;
	private JTextField tfCodInterno;
	private JLabel lblCBarra;
	private ButtonGroup group;
	private JRadioButton rdbtnLista1, rdbtnLista2; 
	private JTextField tfCB;
	
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					PreciosArticulos frame = new PreciosArticulos();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public PreciosArticulos() {
		setTitle("Precios");
		this.setVisible(true);
				
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(0, 0, Principal.ancho, Principal.alto);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
//		JLabel 
		lblCBarra = new JLabel("C. Barra");
		lblCBarra.setBounds(10, 63, 58, 14);
		contentPane.add(lblCBarra);
		
		tfCBarra = new JTextField();
		tfCBarra.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				//controlar los digitos ingresados si estan en la base de datos
				buscaCodBarra();
			}
		});
		tfCBarra.setBounds(38, 32, 120, 20);
		contentPane.add(tfCBarra);
		tfCBarra.setColumns(10);
		
		JLabel lblPrecio = new JLabel("Precio");
		lblPrecio.setBounds(12, 82, 58, 14);
		contentPane.add(lblPrecio);
		
		tfPrecio = new JTextField();
		tfPrecio.setEditable(false);
		tfPrecio.setBounds(12, 98, 58, 20);
		contentPane.add(tfPrecio);
		tfPrecio.setColumns(10);
		
		JLabel lblDescripcin = new JLabel("Descripci\u00F3n");
		lblDescripcin.setBounds(12, 128, 86, 14);
		contentPane.add(lblDescripcin);
		
		tfDescripcion = new JTextArea();
		tfDescripcion.setBounds(12, 145, 162, 35);
		contentPane.add(tfDescripcion);
		tfDescripcion.setEditable(false);
		tfDescripcion.setColumns(10);
		
		JLabel lblCodInterno = new JLabel("Cod. Interno");
		lblCodInterno.setBounds(81, 82, 86, 14);
		contentPane.add(lblCodInterno);
		
		tfCodInterno = new JTextField();
		tfCodInterno.setEditable(false);
		tfCodInterno.setBounds(81, 98, 86, 20);
		contentPane.add(tfCodInterno);
		tfCodInterno.setColumns(10);
		
		JButton btnCancelar = new JButton("Cancelar");
		btnCancelar.setFont(new Font("Tahoma", Font.PLAIN, 9));
		btnCancelar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				//Canselar la carga de articulos
				cancelar();
			}
		});
		btnCancelar.setBounds(95, 217, 79, 23);
		contentPane.add(btnCancelar);
		
		JButton btnBorrar = new JButton("Borrar");
		btnBorrar.setFont(new Font("Tahoma", Font.PLAIN, 10));
		btnBorrar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				//limpiar campos
				borrarCampos();
			}
		});
		btnBorrar.setBounds(17, 217, 72, 23);
		contentPane.add(btnBorrar);
		
		rdbtnLista1 = new JRadioButton("Lista 1", true);
		rdbtnLista1.setBounds(10, 187, 79, 23);
		contentPane.add(rdbtnLista1);
		
		rdbtnLista2 = new JRadioButton("Lista 2", false);
		rdbtnLista2.setBounds(88, 187, 86, 23);
		contentPane.add(rdbtnLista2);
		
		group = new ButtonGroup();
	    group.add(rdbtnLista1);
	    group.add(rdbtnLista2);
	    
	    tfCB = new JTextField();
	    tfCB.setEditable(false);
	    tfCB.setHorizontalAlignment(SwingConstants.CENTER);
	    tfCB.setColumns(10);
	    tfCB.setBounds(67, 60, 100, 20);
	    contentPane.add(tfCB);
	    
	    JSeparator separator = new JSeparator();
	    separator.setBounds(10, 25, 204, 2);
	    contentPane.add(separator);
	    
	    JLabel lblPrecios = new JLabel("Precios");
	    lblPrecios.setFont(new Font("Tahoma", Font.BOLD, 12));
	    lblPrecios.setBounds(7, 5, 105, 20);
	    contentPane.add(lblPrecios);
	}
	
	public void cancelar(){
		this.dispose();
	}
	
	public void buscaCodBarra(){
		try {
			System.out.println("barra del textfield: "+tfCBarra.getText());
			String barra = CodigoBarra.getBarra(tfCBarra.getText());
			Articulo a = ProcCoope.getArticulo(barra);
			if (a != null){
				StringTokenizer str_token = new StringTokenizer (a.getDescripcion());
				String aux = new String(), desc, agregar = new String();
				while ( str_token.hasMoreTokens() )
				{
					desc = str_token.nextToken();
					if(aux.length()+desc.length() < 20){
						aux += " "+desc;
					}else{
						agregar += aux+"\n";
						aux = desc;
					}				    
				}
				agregar += aux+"\n";
				tfCBarra.setText("");
				
				tfCB.setEditable(true);
				tfCB.setText(a.getCodBarra());
				tfCB.setEditable(false);
				
				tfDescripcion.setEditable(true);
				tfDescripcion.setText(agregar);
				tfDescripcion.setEditable(false);
								
				tfCodInterno.setEditable(true);
				tfCodInterno.setText(a.getCodInterno());
				tfCodInterno.setEditable(false);
				
				int lista = 1;
				if (!rdbtnLista1.isSelected()){
					lista = 2;
				}
				
				float iva = FisicaPrecios.getIVA(barra);
				float precio = FisicaPrecios.getPrecio(a.getCodInterno(), lista) ;
				
				tfPrecio.setEditable(true);
				tfPrecio.setText(redondeo(precio*iva));
				tfPrecio.setEditable(false);
				
				tfCBarra.requestFocus(); //le asigno el foco
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	public void borrarCampos(){
		tfCBarra.setText("");
		tfPrecio.setText("");
		tfCodInterno.setText("");
		tfDescripcion.setText("");
		tfCBarra.requestFocus(); //le asigno el foco
	}
	
	public String redondeo(float precio){
		DecimalFormat dfMoney = new DecimalFormat("$0.00");
//		NumberFormat money = NumberFormat.getCurrencyInstance();
		return dfMoney.format(precio);		
	}
}
